package com.wteam.controller;

import com.wteam.framework.common.Wx.service.WxLoginUtil;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.vo.ResultMessage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author doncic
 * @date 2023/7/13 18:26
 */
@Slf4j
@RestController
@Tag(name = "微信扫码登录接口")
@RequestMapping("/member")
public class WxLoginController {

    @Autowired
    private WxLoginUtil wxLoginUtil;

    /**
     * 先获取微信授权链接
     *
     * @return
     */
    @Operation(summary = "取得微信登录授权链接")
    @PostMapping("/getWechatLoginUrl")
    public ResultMessage getWechatLoginUrl() {
        String redirectUrl = wxLoginUtil.getRedirectUrl();
        return ResultUtil.data(redirectUrl);
    }

    /**
     * 处理微信登录回调和获取用户信息
     *
     * @param code
     * @return
     */

    @Operation(summary = "处理微信登录回调和获取用户信息")
    @GetMapping("/callback")
    public ResultMessage handleCallback(
            @RequestParam String code) {
        wxLoginUtil.handleCallback(code);
        return ResultUtil.data(wxLoginUtil.handleCallback(code));
    }
}
